﻿using Newtonsoft.Json.Linq;
using Qianke.Core.Common;
using Qianke.Core.Saas.Manager.ShareApi.Models;

namespace Qianke.Core.Saas.Manager.ShareApi
{
    public static class HttpApi
    {
        private static string _url = "http://saas.ip239.com";
        private static Task<ReturnInfo> ChildApi(string action, object param)
        {
            return ChildApi<JToken>(action, param);
        }

        public static async Task<ReturnInfo> ChildApi<TData>(string action, object param)
        {
            var url = $"{_url}/ChildApi/{action}";
            return await HttpHelper.CallApi<ReturnInfoData<TData>>(url, param);
        }


        public static Task<ReturnInfo> OrderPaySuccess(long orderId, int sumPrice, int payType)
            => ChildApi("OrderPaySuccess", new
            {
                OrderId = orderId,
                SumPrice = sumPrice,
                PayType = payType
            });

        public static Task<ReturnInfo> OutTicket(long managerId, long organId, string keyName = "")
            => ChildApi("OutTicket", new { ManagerId = managerId, OrganId = organId, KeyName = keyName });


        public static Task<ReturnInfo> CancelOrder(long orderId)
            => ChildApi("CancelOrder", new { OrderId = orderId });

        public static Task<ReturnInfo> ChargingOrder(long orderId)
            => ChildApi("ChargingOrder", new { OrderId = orderId });


        public static Task<ReturnInfo> ChangeOrderStatus(long orderId, int status)
            => ChildApi("ChangeOrderStatus", new { OrderId = orderId, Status = status });


        /// <summary>
        /// 退款
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static Task<ReturnInfo> RefundOrder(long orderId)
            => ChildApi("RefundOrder", new { OrderId = orderId });


        public static Task<ReturnInfo> MakeOrder(long organId, long managerId, int platformType, int price, string telephone, int tickCount,
            long memberId, string productName, int cacheMinutes, string getCode, string openId, string cancelUrl)
            => ChildApi("MakeOrder", new
            {
                OpenId = openId,
                OrganId = organId,
                ManagerId = managerId,
                PlatformType = platformType,
                Price = price,
                Telephone = telephone,
                TickCount = tickCount,
                MemberId = memberId,
                ProductName = productName,
                CacheMinutes = cacheMinutes,
                GetCode = getCode,
                CancelUrl = cancelUrl
            });

        public static Task<ReturnInfo> MakeLoginResult(long managerId, long organId, object otherParam) =>
            ChildApi("MakeLoginResult", new { ManagerId = managerId, OrganId = organId, OtherParam = otherParam });

        /// <summary>
        /// 处理白票
        /// </summary>
        /// <returns></returns>
        public static Task<ReturnInfo> CancelTick(long managerId, long organId, long tickNo, string reason) =>
            ChildApi("CancelTick", new { ManagerId = managerId, OrganId = organId, TickNo = tickNo, Reason = reason });


        public static Task<ReturnInfo> SearchOrganPic(long organId, int searchType, Dictionary<string, object> param)
        {
            param = param ?? new Dictionary<string, object>();
            param["SearchType"] = searchType;
            param["OrganId"] = organId;
            return ChildApi("SearchOrganPic", param);
        }

        public static Task<ReturnInfo> SearchManager(long organId, int searchType, Dictionary<string, object> param)
        {
            param = param ?? new Dictionary<string, object>();
            param["SearchType"] = searchType;
            param["OrganId"] = organId;
            return ChildApi("SearchManager", param);
        }

        public static Task<ReturnInfo> SearchOrderInfo(long organId, int searchType, Dictionary<string, object> param)
        {
            param = param ?? new Dictionary<string, object>();
            param["SearchType"] = searchType;
            param["OrganId"] = organId;
            return ChildApi("SearchOrderInfo", param);
        }

        public static Task<ReturnInfo> GetOrganManagerInfo(long organId, long managerId)
        {
            return ChildApi("GetOrganManagerInfo", new
            {
                OrganId = organId,
                ManagerId = managerId,
            });
        }

        public static async Task<string> GetManagerName(long managerId)
        {
            var keyName = $"ManagerName:{managerId}";
            var managerName = CacheHelper.Get(keyName) as string;
            if (managerName == null)
            {
                var result = await ChildApi<string>("GetManagerName", new { ManagerId = managerId });
                if (result.resultcode != "200")
                    return "";
                managerName = (result as ReturnInfoData<string>)?.data;
                if (string.IsNullOrEmpty(managerName))
                    return "";
                CacheHelper.Set(keyName, managerName, 60 * 10);
            }
            return managerName;
        }

        public static async Task<ReturnInfo> GetOrganizeInfo(long organId)
        {
            var keyName = $"OrganizeInfo:{organId}";
            var organModel = CacheHelper.Get(keyName) as Organize;
            if (organModel == null)
            {
                var result = await ChildApi<Organize>("GetOrganizeInfo", new { OrganId = organId });
                if (result.resultcode != "200")
                    return result;
                organModel = (result as ReturnInfoData<Organize>).data;
                CacheHelper.Set(keyName, organModel, 60 * 10);
            }
            return new ReturnInfoData(organModel);
        }


        public static async Task<string> GetOrganName(long organId)
        {
            var result = await GetOrganizeInfo(organId);
            if (result.resultcode != "200")
                return "";
            var organModel = (result as ReturnInfoData).data as Organize;
            return organModel.Name;
        }
    }
}
